package org.comet.auxiliary.client.swing;

import org.comet.auxiliary.client.swing.versions.v1.SettingFrame;
import org.comet.auxiliary.common.util.StringUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TableFrame  extends JDialog implements ActionListener {
    String title;





    @Override
    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        if("clear".equals(actionCommand)){
            try {
                SettingFrame.sqliteHelper.executeUpdate("delete from reddata where userid="+SettingFrame.userInfo.getUid());
                SettingFrame.sqliteHelper.executeUpdate("delete from userdata where userid = "+SettingFrame.userInfo.getUid());
                

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } catch (ClassNotFoundException classNotFoundException) {
                classNotFoundException.printStackTrace();
            }
        }
    }





    public TableFrame(Frame owner, String title, int width, int height){
        super(owner, true);
        this.title = title;
        setBounds(700, 300, width, height);
    }



    public void openFriendList(List<Map<String, Object>> list) {
        setTitle("统计");
        setLayout(new GridLayout(1, 2, 2, 1));
        Object[][] playerInfo = {};
        // 创建表格中的横标题
        String[] Names = { "id", "姓名", "群名", "群号", "发出个数","发出金额","盈亏" };
        // 以Names和playerInfo为参数，创建一个表格
        DefaultTableModel model = new DefaultTableModel(playerInfo, Names);
        for (Map<String, Object> map : list) {
            Object[] data = new Object[]{map.get("phone"),map.get("name"),map.get("groupname"),map.get("groupno"),map.get("sendnum"),map.get("sendmoney"),map.get("yk")};
            model.addRow(data);
        }

        JTable table = new JTable(model);
        // 设置此表视图的首选大小
        table.setPreferredScrollableViewportSize(new Dimension(550, 100));
        // 将表格加入到滚动条组件中
        JScrollPane scrollPane = new JScrollPane(table);
        JButton clear  = new JButton("清空数据");
        clear.setEnabled(true);
        clear.addActionListener(this);
        clear.setActionCommand("clear");
        JPanel jPanel = new JPanel();
        jPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
        jPanel.add(clear);
        jPanel.add(scrollPane);

        Container container = getContentPane();
        container.add(jPanel);
        setVisible(true);



    }

    private void init(List<String> titles) {

    }
}
